package action.common;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;

import persistence.model.TdfkfaPrjFafj;
import service.PlanService;
import action.vo.FileInfo;

import com.opensymphony.xwork2.ActionSupport;

public class FileBrowseAction extends ActionSupport {

    private static final long serialVersionUID = -1588269602148257344L;

    private List<String> fileLinkList;

    private List<FileInfo> fileInfoList;

    private PlanService planService;

    private Integer planId;

    private String type;

    private List<TdfkfaPrjFafj> fafjList;

    public String loadDxtpLinks() throws Exception {

        HttpServletRequest request = ServletActionContext.getRequest();

        String subPath = DigestUtils.shaHex(planId.toString());
        String filePath = request.getSession().getServletContext().getRealPath("planFile\\dxtp\\" + subPath) + "\\";

        List<String> extList = new ArrayList<String>();
        extList.add(".jpg");
        extList.add(".gif");
        extList.add(".png");
        extList.add(".bmp");

        String linkPrefix = request.getContextPath() + "/planFile/dxtp/" + subPath + "/";
        fileInfoList = new ArrayList<FileInfo>();

        String[] fileNames = getFileNames(filePath, extList);
        if (fileNames != null) {
            for (String name : fileNames) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(name);
                fileInfo.setUrl(linkPrefix + name);
                fileInfoList.add(fileInfo);
            }
        }

        return SUCCESS;
    }

    public String loadFafj() throws Exception {
        fafjList = planService.loadFafj(planId, type);
        return SUCCESS;
    }

    public String loadAllFafj() throws Exception {
        fafjList = planService.loadAllFafj(planId);
        return SUCCESS;
    }

    private String[] getFileNames(String filePath, final List<String> extList) {

        File dir = new File(filePath);
        String[] links = dir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {

                for (String ext : extList) {
                    if (name.toUpperCase().endsWith(ext.toUpperCase())) {
                        return true;
                    }
                }
                return false;
            }

        });

        return links;
    }

    public List<String> getFileLinkList() {
        return fileLinkList;
    }

    public void setPlanService(PlanService planService) {
        this.planService = planService;
    }

    public List<TdfkfaPrjFafj> getFafjList() {
        return fafjList;
    }

    public List<FileInfo> getFileInfoList() {
        return fileInfoList;
    }

    public void setPlanId(Integer planId) {
        this.planId = planId;
    }

    public void setType(String type) {
        this.type = type;
    }

}
